﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Migrator.Entities;
using Migrator.Services;
using Migrator.Services.MobileAgents;
using Migrator.Util;

namespace MigratorUI.UI
{
    public partial class NodePropertiesControl : UserControl
    {
        public NodePropertiesControl()
        {
            InitializeComponent();
            this.Enabled = false;
        }

        public void LoadNode(Node node)
        {
            if (node == null)
                return;
            if (node is Router)
            {
                this.Enabled = true;
                this.node = node;
                InitializeParameters();
            }
        }

        private void InitializeParameters()
        {
            tbRadius.Value = this.node.Radius;
            lbRadius.Text = this.node.Radius + "";
            this.lbX.Text = this.node.Location.X + "";
            this.lbY.Text = this.node.Location.Y + "";
            
            this.txtMACAddress.Text = this.node.MACAddress.ToString();
            this.txtIPAddress.Text = this.node.IPAddress.ToString();
            
            this.InitializeAsMobileAgent();
        }

        private void InitializeAsMobileAgent()
        {
            this.lvForeignTable.Items.Clear();
            this.lvHomeTable.Items.Clear();

            this.node = node as Router;
            this.Text = "Mobile Agent properties";

            HomeAgent homeAgentService = (HomeAgent)node.GetService(Services.HomeAgent);
            ForeignAgent foreignAgentService = (ForeignAgent)node.GetService(Services.ForeignAgent);

            cBHomeAgent.Checked = homeAgentService == null ? false : homeAgentService.Enabled;
            cBForeignAgent.Checked = node.GetService(Services.ForeignAgent).Enabled;
            cBBindingCache.Checked = node.GetService(Services.RouteOptimizationService).Enabled;
            cBDHCPServer.Checked = node.GetService(Services.DHCPServer).Enabled;

            if (homeAgentService != null)
            {
                if (homeAgentService.Enabled)
                {
                    foreach (var entries in homeAgentService.BindingTable.Table)
                    {
                        foreach (var entry in entries.Value)
                        {
                            string[] items = new string[]
                            {
                                entry.HomeAddress.ToString(),
                                entry.CareOfAddress.ToString(),
                                entry.ExpirationTime + ""
                            };
                            ListViewItem lvItem = new ListViewItem(items);
                            lvHomeTable.Items.Add(lvItem);
                        }
                    }
                }
                else
                {
                    homeAgentTab = PropertiesTab.TabPages[2];
                    PropertiesTab.TabPages.RemoveAt(2);
                }
            }

            if (foreignAgentService != null)
            {
                if (foreignAgentService.Enabled)
                {
                    foreach (var entry in foreignAgentService.VisitorsTable.Table)
                    {
                        VisitorsTable.VisitorsEntry bindingEntry = entry.Value;
                        string[] items = new string[]
                        {
                            bindingEntry.HomeAddress.ToString(),
                            bindingEntry.HomeAgent.ToString(),
                            bindingEntry.MACAddress.ToString(),
                            bindingEntry.ExpirationTime + ""
                        };
                        ListViewItem lvItem = new ListViewItem(items);
                        lvForeignTable.Items.Add(lvItem);
                    }
                }
                else
                {
                    if (PropertiesTab.TabPages.Count < 4)
                        PropertiesTab.TabPages.RemoveAt(2);
                    else
                        PropertiesTab.TabPages.RemoveAt(3);
                }
            }
        }


        private void tbRadius_Scroll(object sender, EventArgs e)
        {
            this.lbRadius.Text = this.tbRadius.Value + "";
            this.node.Radius = this.tbRadius.Value;
            if (this.PropertyChangedEvent != null) this.PropertyChangedEvent.Invoke(this.node);
        }

        /*
        private void btnApplySettings_Click(object sender, EventArgs e)
        {
            HomeAgent homeAgentService = (HomeAgent)node.GetService(Services.HomeAgent);
            ForeignAgent foreignAgentService = (ForeignAgent)node.GetService(Services.ForeignAgent);
            IService routeOptimizationService = node.GetService(Services.RouteOptimizationService);

            if (foreignAgentService != null)
                foreignAgentService.Enabled = this.cBForeignAgent.Checked;

            if (homeAgentService != null)
                homeAgentService.Enabled = this.cBHomeAgent.Checked;

            if (routeOptimizationService != null)
                routeOptimizationService.Enabled = false;

            this.node.Radius = tbRadius.Value;
        }
         * */

        private Node node;
        private TabPage homeAgentTab;
        public delegate void PropertyChangedEventHandler(Node node);
        public event PropertyChangedEventHandler PropertyChangedEvent;
    }
}
